home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 13.0 KB | 422 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWObjecH.h
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWOBJECH_H
- #define FWOBJECH_H
-
- #ifndef FWPLATME_H
- #include <FWPlatMe.h>
- #endif
-
- #ifndef FWMEMORH_H
- #include <FWMemorH.h>
- #endif
-
- #ifndef FWBESTFH_H
- #include <FWBestFH.h>
- #endif
-
-
- //========================================================================================
- // Forward class declarations
- //========================================================================================
-
- class FW_CChunk;
- class FW_CPrivChunkyBlock;
- class FW_CObjectHeap;
- class FW_CPrivChunkyBlockStack;
-
-
- //========================================================================================
- // CLASS FW_CPrivChunkyBlock
- //========================================================================================
-
- #ifdef BUILD_WIN
- // Bytes are in reverse order in a word.
-
- const unsigned short ChunkyBlock_kSizeIndexMask = 0x00F0;
- const unsigned short ChunkyBlock_kSizeIndexShift = 4;
-
- const unsigned short ChunkyBlock_kBlockIndexMask = 0x000F;
- const unsigned short ChunkyBlock_kBlockIndexShift = 0;
-
- const unsigned short ChunkyBlock_kBlockTypeMask = 0xF000;
- const unsigned short ChunkyBlock_kBlockTypeShift = 12;
-
- const unsigned short ChunkyBlock_kMagicNumberMask = 0x0F00;
- const unsigned short ChunkyBlock_kMagicNumberShift = 8;
- #else
- const unsigned short ChunkyBlock_kSizeIndexMask = 0xF000;
- const unsigned short ChunkyBlock_kSizeIndexShift = 12;
-
- const unsigned short ChunkyBlock_kBlockIndexMask = 0x0F00;
- const unsigned short ChunkyBlock_kBlockIndexShift = 8;
-
- const unsigned short ChunkyBlock_kBlockTypeMask = 0x00F0;
- const unsigned short ChunkyBlock_kBlockTypeShift = 4;
-
- const unsigned short ChunkyBlock_kMagicNumberMask = 0x000F;
- const unsigned short ChunkyBlock_kMagicNumberShift = 0;
- #endif
-
- class FW_CPrivChunkyBlock
- {
- public:
- enum
- {
- kBusyOverhead = sizeof(unsigned short),
- kBlockTypeId = FW_CPrivBestFitBlock::kBlockTypeId + 1,
- kMagicNumber = 0xA
- };
-
- void *operator new(SIZE_T, void *ptr);
- void *operator new(SIZE_T);
- void operator delete(void *) { };
-
- FW_CPrivChunkyBlock();
- FW_CPrivChunkyBlock(unsigned int sizeIndex, unsigned int blockIndex);
- FW_CPrivChunkyBlock(const FW_CPrivChunkyBlock& blk);
- FW_CPrivChunkyBlock& operator=(const FW_CPrivChunkyBlock& blk);
-
- unsigned short GetSizeIndex() const;
- void SetSizeIndex(unsigned short index);
-
- unsigned short GetBlockIndex() const;
- void SetBlockIndex(unsigned short index);
-
- unsigned short GetBlockType() const;
- void SetBlockType(unsigned short type);
-
- unsigned short GetMagicNumber() const;
- void SetMagicNumber(unsigned short magic);
-
- FW_CChunk* GetChunk(FW_BlockSize blkSize);
-
- FW_CPrivChunkyBlock* GetNext();
- void SetNext(FW_CPrivChunkyBlock* blk);
-
- Boolean IsBusy(FW_BlockSize blockSize);
- void SetBusy(FW_BlockSize blockSize, Boolean busy);
-
- private:
- // Fields present in both free and busy blocks. Several bit fields are stored in
- // the following fields. They are accessed using get and set methods.
-
- unsigned short fBits;
-
- // Fields present in only free blocks.
-
- FW_CPrivChunkyBlock* fNext;
- };
-
-
- //========================================================================================
- // CLASS FW_CPrivChunkyBlockStack
- //========================================================================================
-
- class FW_CPrivChunkyBlockStack
- {
- public:
- FW_CPrivChunkyBlockStack();
- FW_CPrivChunkyBlockStack(const FW_CPrivChunkyBlockStack& blk);
- ~FW_CPrivChunkyBlockStack();
- FW_CPrivChunkyBlockStack& operator=(const FW_CPrivChunkyBlockStack& blk);
-
- FW_CPrivChunkyBlock* Pop();
- void Push(FW_CPrivChunkyBlock* blk);
- void RemoveRange(void *begAddr, void *endAddr);
- FW_CPrivChunkyBlock* Top();
-
- private:
- FW_CPrivChunkyBlock fHead;
- };
-
-
- //========================================================================================
- // CLASS FW_CChunk
- //========================================================================================
-
- struct FW_SPrivChunkHeader
- {
- unsigned short fBlockBusyBits;
- };
-
- class FW_CChunk
- {
- public:
- FW_CChunk(short blocksPerChunk,
- unsigned int sizeIndex,
- FW_BlockSize blockSize);
-
- void *operator new(SIZE_T, void* ptr);
- void *operator new(SIZE_T);
- void operator delete(void *) { };
-
- FW_CPrivChunkyBlock* GetBlock(unsigned int blkIndex, FW_BlockSize blkSize);
- unsigned int GetSizeIndex();
-
- Boolean IsBlockBusy(unsigned int whichBlock);
- Boolean IsBusy();
-
- void SetBlockBusy(unsigned int whichBlock, Boolean busy);
-
- private:
- FW_SPrivChunkHeader fHeader;
-
- FW_CChunk(const FW_CChunk& blk);
- FW_CChunk& operator=(const FW_CChunk& blk);
- // This class shouldn't be copied.
- };
-
-
- //========================================================================================
- // CLASS FW_CObjectHeap
- //========================================================================================
-
- const FW_BlockSize ObjectHeap_kDefaultBlockSizes[] = {sizeof(FW_CPrivChunkyBlock), 10, 14, 18, 0};
-
- class FW_CObjectHeap : public FW_CBestFitHeap
- {
- private:
-
- public:
- enum
- {
- kMaxNumberOfBlockSizes = 16,
- kDefaultBlocksPerChunk = 4,
- kDefaultInitialSize = 10240,
- kDefaultIncrementSize = 4096
- };
-
- FW_CObjectHeap(unsigned long initialSize,
- unsigned long incrementSize = 0,
- short blocksPerChunk = kDefaultBlocksPerChunk);
-
- FW_CObjectHeap(const FW_BlockSize* blockSizes = ObjectHeap_kDefaultBlockSizes,
- unsigned long initialSize = kDefaultInitialSize,
- unsigned long incrementSize = kDefaultIncrementSize,
- short blocksPerChunk = kDefaultBlocksPerChunk);
-
- void IObjectHeap();
-
- virtual ~FW_CObjectHeap();
-
- protected:
- virtual void* DoAllocate(FW_BlockSize size, FW_BlockSize& allocatedSize);
- virtual FW_BlockSize DoBlockSize(const void* block) const;
- virtual void DoFree(void*);
- virtual void DoReset();
-
- void* AllocateBlock(unsigned int sizeIndex);
- void CreateNewChunk(unsigned int sizeIndex);
- void FreeBlock(FW_CPrivChunkyBlock* blk);
- unsigned int SizeIndex(FW_BlockSize size);
-
- #ifdef FW_DEBUG
- virtual void CompilerCheck();
- virtual Boolean DoIsValidBlock(void* blk) const;
- #endif
-
- private:
-
- short fNumberOfBlockSizes;
- const FW_BlockSize* fBlockSizes;
- FW_CPrivChunkyBlockStack fFreeLists[kMaxNumberOfBlockSizes];
- short fBlocksPerChunk;
-
- FW_CObjectHeap(const FW_CObjectHeap& blk);
- FW_CObjectHeap& operator=(const FW_CObjectHeap& blk);
- // This class shouldn't be copied.
- };
-
-
- //========================================================================================
- // CLASS FW_CPrivChunkyBlock
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::operator new
- //----------------------------------------------------------------------------------------
-
- inline void *FW_CPrivChunkyBlock::operator new(SIZE_T, void *ptr)
- {
- return ptr;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::operator new
- //----------------------------------------------------------------------------------------
-
- inline void *FW_CPrivChunkyBlock::operator new(SIZE_T)
- {
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::FW_CPrivChunkyBlock
- //----------------------------------------------------------------------------------------
-
- inline FW_CPrivChunkyBlock::FW_CPrivChunkyBlock(const FW_CPrivChunkyBlock& blk) :
- fBits(blk.fBits),
- fNext(blk.fNext)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::operator=
- //----------------------------------------------------------------------------------------
-
- inline FW_CPrivChunkyBlock& FW_CPrivChunkyBlock::operator=(const FW_CPrivChunkyBlock& blk)
- {
- fBits = blk.fBits;
- fNext = blk.fNext;
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::GetSizeIndex
- //----------------------------------------------------------------------------------------
-
- inline unsigned short FW_CPrivChunkyBlock::GetSizeIndex() const
- {
- return (fBits & ChunkyBlock_kSizeIndexMask) >> ChunkyBlock_kSizeIndexShift;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::SetSizeIndex
- //----------------------------------------------------------------------------------------
-
- inline void FW_CPrivChunkyBlock::SetSizeIndex(unsigned short index)
- {
- fBits &= ~ChunkyBlock_kSizeIndexMask;
- fBits |= (index << ChunkyBlock_kSizeIndexShift) & ChunkyBlock_kSizeIndexMask;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::GetBlockIndex
- //----------------------------------------------------------------------------------------
-
- inline unsigned short FW_CPrivChunkyBlock::GetBlockIndex() const
- {
- return (fBits & ChunkyBlock_kBlockIndexMask) >> ChunkyBlock_kBlockIndexShift;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::SetBlockIndex
- //----------------------------------------------------------------------------------------
-
- inline void FW_CPrivChunkyBlock::SetBlockIndex(unsigned short index)
- {
- fBits &= ~ChunkyBlock_kBlockIndexMask;
- fBits |= (index << ChunkyBlock_kBlockIndexShift) & ChunkyBlock_kBlockIndexMask;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::GetBlockType
- //----------------------------------------------------------------------------------------
-
- inline unsigned short FW_CPrivChunkyBlock::GetBlockType() const
- {
- return (fBits & ChunkyBlock_kBlockTypeMask) >> ChunkyBlock_kBlockTypeShift;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::SetBlockType
- //----------------------------------------------------------------------------------------
-
- inline void FW_CPrivChunkyBlock::SetBlockType(unsigned short type)
- {
- fBits &= ~ChunkyBlock_kBlockTypeMask;
- fBits |= (type << ChunkyBlock_kBlockTypeShift) & ChunkyBlock_kBlockTypeMask;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::GetMagicNumber
- //----------------------------------------------------------------------------------------
-
- inline unsigned short FW_CPrivChunkyBlock::GetMagicNumber() const
- {
- return (fBits & ChunkyBlock_kMagicNumberMask) >> ChunkyBlock_kMagicNumberShift;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::SetMagicNumber
- //----------------------------------------------------------------------------------------
-
- inline void FW_CPrivChunkyBlock::SetMagicNumber(unsigned short magic)
- {
- fBits &= ~ChunkyBlock_kMagicNumberMask;
- fBits |= (magic << ChunkyBlock_kMagicNumberShift) & ChunkyBlock_kMagicNumberMask;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::GetNext
- //----------------------------------------------------------------------------------------
-
- inline FW_CPrivChunkyBlock* FW_CPrivChunkyBlock::GetNext()
- {
- return fNext;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivChunkyBlock::SetNext
- //----------------------------------------------------------------------------------------
-
- inline void FW_CPrivChunkyBlock::SetNext(FW_CPrivChunkyBlock* blk)
- {
- fNext = blk;
- }
-
-
- //========================================================================================
- // CLASS FW_CChunk
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CChunk::operator new
- //----------------------------------------------------------------------------------------
-
- inline void* FW_CChunk::operator new(SIZE_T, void *ptr)
- {
- return ptr;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CChunk::operator new
- //----------------------------------------------------------------------------------------
-
- inline void* FW_CChunk::operator new(SIZE_T)
- {
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CChunk::GetSizeIndex
- //----------------------------------------------------------------------------------------
-
- inline unsigned int FW_CChunk::GetSizeIndex()
- {
- FW_CPrivChunkyBlock *block
- = (FW_CPrivChunkyBlock *) ((FW_BytePtr) this + sizeof(FW_SPrivChunkHeader));
- return block->GetSizeIndex();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CChunk::IsBusy
- //----------------------------------------------------------------------------------------
-
- inline Boolean FW_CChunk::IsBusy()
- {
- return fHeader.fBlockBusyBits != 0;
- }
-
- #endif
-